package com.jindi.appuser.service.impl;

import com.jindi.appuser.common.EmptyUtil;
import com.jindi.appuser.common.ServerResponse;
import com.jindi.appuser.dao.AppInfoMapper;
import com.jindi.appuser.domain.AppInfo;
import com.jindi.appuser.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: duanj
 * Date: 2018-07-10
 * Time: 15:26
 * Description:
 */
@Service
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    private AppInfoMapper appInfoMapper;

    @Override
    public ServerResponse appInfoList(String  type, String os) {
        List<AppInfo> list = appInfoMapper.list(type,os);
        if(list==null||list.size()<1){
            return ServerResponse.createByErrorMessage("数据有误");
        }
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse addAppInfo(String name, String size, String publishTime, String downUrl, String iconUrl, String connect, String mode, String type, String os) {
        AppInfo appInfo = new AppInfo(null,name,size,publishTime,os,downUrl,iconUrl,type,mode,connect);
        int index = appInfoMapper.insertSelective(appInfo);
        if (index>0){
            return  ServerResponse.createBySuccessMessage("添加成功");
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }

    @Override
    public ServerResponse updateAppInfoById(Integer id , String name, String size, String publishTime, String downUrl, String iconUrl, String connect, String mode, String type, String os) {
        AppInfo appInfo = new AppInfo(id,name,size,publishTime,os,downUrl,iconUrl,type,mode,connect);
        int index = appInfoMapper.updateByPrimaryKeySelective(appInfo);
        if (index>0){
            return  ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Override
    public ServerResponse delAppInfoById(Integer id) {
        int index = appInfoMapper.deleteByPrimaryKey(id);
        if (index>0){
            return  ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    @Override
    public ServerResponse getAppInfoById(Integer id) {
        AppInfo appInfo = appInfoMapper.selectByPrimaryKey(id);
        if(EmptyUtil.isEmpty(appInfo)){
            return ServerResponse.createByErrorMessage("数据有误");
        }
        return ServerResponse.createBySuccess(appInfo);
    }

    /*@Override
    public ServerResponse updateAppInfoById(AppInfo app) {
        int index = appInfoMapper.updateByPrimaryKeySelective(app);
        if (index>0){
            return  ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }*/
}
